<?php

/**
 * [PHPFOX_HEADER]
 */
defined('PHPFOX') or exit('NO DICE!');

/**
 * 
 * 
 * @copyright		[PHPFOX_COPYRIGHT]
 * @author  		Raymond Benc
 * @package 		Phpfox_Component
 * @version 		$Id: index.class.php 300 2009-03-20 13:01:54Z Raymond_Benc $
 */
class Takepicture_Component_Controller_Upload extends Phpfox_Component {

    /**
     * Class process method wnich is used to execute this component.
     */
    public function process() {
        
        
        //method POST
        //$_FILES['image'];
        $sDirImage = '';
        $sUrlImage = '';

        if (isset($_FILES['image']) && $_FILES['image']['error'] == UPLOAD_ERR_OK) {
            //load image
            $aImage = Phpfox::getLib('file')->load('image', array('jpg', 'gif', 'png'), NULL);

            //upload image
            if (Phpfox_Error::isPassed()) {
                $sFileName = Phpfox::getLib('file')->upload('image', Phpfox::getParam('takepicture.dir_pic'), uniqid(), true, 0644, true, false);

                $sFileName = sprintf($sFileName, '');
                $sDirImage = Phpfox::getParam('takepicture.dir_pic') . $sFileName;
                $sUrlImage = Phpfox::getParam('takepicture.url_pic') . $sFileName;
            } else {
                die(1);
            }
        }

        //$aVals = array(
        //  'social' => array(),
        //  'message' => string,
        //);
        if (($aVals = $this->request()->getArray('val'))) {
            $aListSocial = $aVals['social'];
            $sMessage = $aVals['message'];

            $aRows = Phpfox::getService('takepicture')->getListSocial($aListSocial);

            if ($aRows) :

                foreach ($aRows as $item) :
                    //restore session
                    Phpfox::getService('loginsocial.process')->initHandle(array(
                        'session_data' => $item['session_data'],
                        'provider' => $item['provider']
                    ));

                    //check connect with current social
                    if (Phpfox::getService('loginsocial.process')->getHybridAuth()->isConnectedWith($item['provider'])) {
                        
                        //post photo
                        $aReponse = Phpfox::getService('loginsocial.process')->getAdapter()->postPhoto(array(
                            'dir' => $sDirImage,
                            'url' => $sUrlImage,
                            'message' => $aVals['message'],
                        ));

                        //log result.
                        if ($aReponse['success'] == 1) {
                            Phpfox::log('Post to ' . $item['provider'] . ' successfully');
                        } else {
                            Phpfox::log('Post to ' . $item['provider'] . ' fail: ' . $aReponse['output']);
                        }
                    }
                endforeach;

            endif;
            @unlink($sDirImage);
        }
    }

    /**
     * Garbage collector. Is executed after this class has completed
     * its job and the template has also been displayed.
     */
    public function clean() {
        
    }

}

?>